//
//  vdAtlasShapeContainer.m
//  Void Dead
//
//  Created by Sidney Just on 20.12.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdAtlasShapeContainer.h"

// \cond
@interface vdShapeContainer (_private)

- (int)appendShape;

@end
// \endcond

@implementation vdAtlasShapeContainer

- (void)calculateSize {
	size_x = 0;
	size_y = 0;
	
	for(int i=0; i<shapeCount; i++)
	{
		if(shape[i]->pos_x + shape[i]->size_x > size_x)
			size_x = shape[i]->pos_x + shape[i]->size_x;
		
		if(shape[i]->pos_y + shape[i]->size_y > size_y)
			size_y = shape[i]->pos_y + shape[i]->size_y;
	}
}


- (vdInlineShape *)addShape:(CGPoint)point withAtlas:(CGRect)rect {
	int i = [self appendShape];
	
	shape[i]->pos_x = point.x;
	shape[i]->pos_y = point.y;
	
	shape[i]->rotation = 0.0f;
	
	if(texture)
	{
		texCoords[(i*12)+0] = rect.origin.x / texture.dimension_x;
		texCoords[(i*12)+1] = (rect.origin.y + rect.size.height) / texture.dimension_y;
		
		texCoords[(i*12)+2] = (rect.origin.x + rect.size.width) / texture.dimension_x; 
		texCoords[(i*12)+3] = (rect.origin.y + rect.size.height) / texture.dimension_y;
		
		texCoords[(i*12)+4] = rect.origin.x / texture.dimension_x; 
		texCoords[(i*12)+5] = rect.origin.y / texture.dimension_y;
		
		texCoords[(i*12)+8] = (rect.origin.x + rect.size.width) / texture.dimension_x; 
		texCoords[(i*12)+9] = rect.origin.y / texture.dimension_y;
		
		texCoords[(i*12)+6] = (rect.origin.x + rect.size.width) / texture.dimension_x; 
		texCoords[(i*12)+7] = (rect.origin.y + rect.size.height) / texture.dimension_y;
		
		texCoords[(i*12)+10] = rect.origin.x / texture.dimension_x; 
		texCoords[(i*12)+11] = rect.origin.y / texture.dimension_y;
	}
	
	shape[i]->scale_x = 1;
	shape[i]->scale_y = 1;
	
	shape[i]->key = 0;
	
	shape[i]->size_x = rect.size.width;
	shape[i]->size_y = rect.size.height;
	
	shape[i]->atlas_x = rect.origin.x;
	shape[i]->atlas_y = rect.origin.y;
	
	[self calculateSize];
	return shape[i];
}

- (vdInlineShape *)addShape:(CGPoint)point withAtlas:(CGRect)rect forKey:(int)key {
	vdInlineShape *temp = [self addShape:point withAtlas:rect];
	temp->key = key;
	return temp;
}

- (vdInlineShape *)addShape:(CGPoint)point {
	int i = [self appendShape];
	
	shape[i]->pos_x = point.x;
	shape[i]->pos_y = point.y;
	
	if(texture)
	{
		shape[i]->size_x = texture.dimension_x;
		shape[i]->size_y = texture.dimension_y;
	}
	
	shape[i]->scale_x = 1;
	shape[i]->scale_y = 1;
	
	shape[i]->key = 0;
	
	texCoords[(i*12)+0] = 0;
	texCoords[(i*12)+1] = 1;
	
	texCoords[(i*12)+2] = 1; 
	texCoords[(i*12)+3] = 1;
	
	texCoords[(i*12)+4] = 0; 
	texCoords[(i*12)+5] = 0;
	
	texCoords[(i*12)+8] = 1; 
	texCoords[(i*12)+9] = 0;
	
	texCoords[(i*12)+6] = 1; 
	texCoords[(i*12)+7] = 1;
	
	texCoords[(i*12)+10] = 0; 
	texCoords[(i*12)+11] = 0;
	
	shape[i]->atlas_x = 0;
	shape[i]->atlas_y = 0;
	
	[self calculateSize];
	return shape[i];
}

- (void)updateAtlas:(CGRect)rect forShape:(vdInlineShape *)_shape {
	for(int i=0; i<shapeCount; i++)
	{
		if(shape[i] == _shape)
		{
			shape[i]->size_x = rect.size.width;
			shape[i]->size_y = rect.size.height;
			
			shape[i]->atlas_x = rect.origin.x;
			shape[i]->atlas_y = rect.origin.y;
			
			texCoords[(i*12)+0] = shape[i]->atlas_x / texture.dimension_x;
			texCoords[(i*12)+1] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+2] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+3] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+4] = shape[i]->atlas_x / texture.dimension_x; 
			texCoords[(i*12)+5] = shape[i]->atlas_y / texture.dimension_y;
			
			texCoords[(i*12)+8] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+9] = shape[i]->atlas_y / texture.dimension_y;
			
			texCoords[(i*12)+6] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+7] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+10] = shape[i]->atlas_x / texture.dimension_x; 
			texCoords[(i*12)+11] = shape[i]->atlas_y / texture.dimension_y;
			
			return;
		}
	}
}

- (void)setTexture:(vdTexNode *)tex {
	texture = tex;
	
	if(texture)
	{
		for(int i=0; i<shapeCount; i++)
		{
			texCoords[(i*12)+0] = shape[i]->atlas_x / texture.dimension_x;
			texCoords[(i*12)+1] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+2] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+3] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+4] = shape[i]->atlas_x / texture.dimension_x; 
			texCoords[(i*12)+5] = shape[i]->atlas_y / texture.dimension_y;
			
			texCoords[(i*12)+8] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+9] = shape[i]->atlas_y / texture.dimension_y;
			
			texCoords[(i*12)+6] = (shape[i]->atlas_x + shape[i]->size_x) / texture.dimension_x; 
			texCoords[(i*12)+7] = (shape[i]->atlas_y + shape[i]->size_y) / texture.dimension_y;
			
			texCoords[(i*12)+10] = shape[i]->atlas_x / texture.dimension_x; 
			texCoords[(i*12)+11] = shape[i]->atlas_y / texture.dimension_y;
		}
	}
}

- (id)init {
	if(self = [super init])
	{
		
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

@end
